import { listenCssVar_mana } from '../../../../../../../global/element/css/Func/listenCssVar/classes/cla_listenCssVar_mana';
import { create_ListenEvent_mana } from '../../../../../../../global/element/Event/listenEvent_mana';
// eslint-disable-next-line no-unused-vars
import { unit_listenEvent_mana } from '../../../../../../../global/element/Event/listenEvent_mana/Classes/cla_unit_listenEvent_mana';
import {
  appearElFunc,
  disappearElFunc,
  normalizeContainer,
  packNode,
} from '../../../../../../../global/element/Func';
import { toStringTypeCheck } from '../../../../../../../global/Func/uniFunc/Func';
import { create_static_focusDisplay_mana } from '../../../../../../globalComps/comps/focusDisplay';
// eslint-disable-next-line no-unused-vars
import { focusDisplay_operate } from '../../../../../../globalComps/comps/focusDisplay/Classes/cla_focusDisplay_operate/cla_focusDisplay_operate';
import { fsl_comps_boder_id } from '../comps/float_setting_list/DataDefine/sourceData';
import {
  main_float_setting_id,
  spacename_float_setting,
} from '../DataDefine/sourceData';
import { cla_float_setting_eventBind } from './cla_float_setting_eventBind';

const cla_float_setting_dataManaTag = 'cla_float_setting_dataMana';

/**
 * 浮动元素 全局管理集 目标注册
 */
let focusDisplay_spacename = create_static_focusDisplay_mana(
  spacename_float_setting
);

class cla_float_setting_dataMana {
  get [Symbol.toStringTag]() {
    return cla_float_setting_dataManaTag;
  }

  constructor() {
    this.focusDisplay_op =
      focusDisplay_spacename.getOrCreate_focusDisplay_dom();

    this.focusDisplay_el = this.focusDisplay_op.targetEl;

    this.focusDisplay_el_container = this.focusDisplay_el.firstChild;

    this.cssVar_obj_focusDisplay = new listenCssVar_mana(this.focusDisplay_el);
  }

  /**
   * 特定 注入 this 绑定事件
   * @type {cla_float_setting_eventBind}
   */
  eventBind = new cla_float_setting_eventBind();

  /**
   * 事件 监听管理
   * @type {unit_listenEvent_mana}
   */
  eventM = new create_ListenEvent_mana(spacename_float_setting);

  /**
   * css 变量 监控 -- 焦点元素
   * @type {listenCssVar_mana}
   */
  cssVar_obj_focusDisplay;

  /**
   * 主体元素
   * @type {HTMLElement}
   */
  main_float_setting_el = normalizeContainer(main_float_setting_id);

  /**
   * setting_list 容器 元素
   * @type {HTMLElement}
   */
  fsl_comps_boder_el = normalizeContainer(fsl_comps_boder_id);

  /**
   * 焦点元素 操作集
   * @type {focusDisplay_operate}
   */
  focusDisplay_op;

  /**
   * 焦点元素
   * @type {HTMLDialogElement}
   */
  focusDisplay_el;

  /**
   * 焦点元素 容器
   * @type {HTMLElement}
   */
  focusDisplay_el_container;

  /**
   * 将 setting_list 子元素 打包到 焦点元素
   */
  packNode_setting_list_children_to_focusDisplay_el() {
    do {
      const child_node = this.fsl_comps_boder_el.firstChild;
      // 打包 元素 到 焦点元素
      packNode(this.focusDisplay_el_container, child_node);

      // 应用 显示放大 动画
      appearElFunc(child_node, 'both');
    } while (this.fsl_comps_boder_el.firstChild != null);
  }

  /**
   * 将焦点元素 setting_list 子元素 打包到 setting_list
   */
  packNode_focusDisplay_el_to_setting_list_children(cb) {
    let promise_arr = [];

    for (const child_node of this.focusDisplay_el_container.children) {
      let p = new Promise((resolve) => {
        // 应用 显示放大 动画
        disappearElFunc(child_node, 'both', () => {
          // 打包 元素 到 setting_list
          packNode(this.fsl_comps_boder_el, child_node);
          resolve();
        });
      });

      promise_arr.push(p);
    }

    Promise.all(promise_arr).then(() => {
      if (toStringTypeCheck(cb, 'function')) {
        cb();
      }
    });
  }
}

export { cla_float_setting_dataMana };
